//Date range picker with time picker
$('#duration_time').daterangepicker({
	timePicker : true,
	timePickerIncrement : 30,
	separator: '~',
	showDropdowns: true,
	format : 'YYYY-MM-DD HH:mm'
});

var controller = {
	/**
	 * 初始化
	 */
	init : function() {
		var _this = this;
		_this.bindClick();
	},
	/**
	 * 绑定事件
	 */
	bindEvent : function() {
		var _this = this;
		_this.bindClick()
	},
	bindClick : function() {

		$("#addDialogBtn")
				.bind(
						"click",
						function() {
							$("#editWarningInfoBtn").addClass("hide");
							$("#addWarningInfoBtn").removeClass("hide");

							var warning_concepts = ajax.load(
									"/conceptcode?classification=2", "查询标签失败");
							App.seSelectConcept("warning_classification",
									warning_concepts);

							var level_concepts = ajax.load(
									"/conceptcode?classification=3", "查询标签失败");
							App.seSelectConcept("level_classification",
									level_concepts);
							
							loadArea();

							$("#addDialog").modal('show');
						});
		
		$("#search_area_name").change(function () {
			loadArea();
        });
		
		function loadArea() {
			var areaName = $("#search_area_name").val();
			var areaData = ajax.load("/areas?rowSize=10&areaName="+areaName, "查询区域信息失败");
			$("#area_id").html("");
			for (var i = 0; i < areaData.length; i++) {
				$("#area_id").append(
						"<option value='" + areaData[i].area_id
								+ "'>" + areaData[i].area_name
								+ "</option>");
			}
			
		}

		$("#addWarningInfoBtn").bind(
				"click",
				function() {
					$("#addDialog").modal('hide');
					// 资源属性
					var warning_classification = $(
							'#warning_classification option:selected').val();
					if (warning_classification == "unselect") {
						App.myDialog("请选择预警类型", "提示");
						return;
					}
					var level_classification = $(
							'#level_classification option:selected').val();
					if (level_classification == "unselect") {
						App.myDialog("请选择预警级别", "提示");
						return; 
					}
					var warning_content = $("#warning_content").val();
					var area_id = $('#area_id option:selected').val();
					if (area_id == "unselect") {
						App.myDialog("请选择区域信息", "提示");
						return; 
					}
					var duration_time = $("#duration_time").val();
					if(typeof(duration_time) == "undefined" || duration_time == "") {
						App.myDialog("请选择影响时间", "提示");
						return; 
					}
					var start_time = duration_time.split("~")[0] + ":00";
					var end_time = duration_time.split("~")[1] + ":00";

					var warninginfo = {
						warning_classification : "2",
						level_classification : "3",
						warning_concept : warning_classification,
						level_concept : level_classification,
						warning_content : warning_content,
						area_id : area_id,
						start_time : start_time,
						end_time : end_time
					}
					var warninginfoString = JSON.stringify(warninginfo);
					ajax.addOrUpdate("/warninginfo/add", warninginfoString, "添加预警信息成功！", "添加预警信息失败：");
				});
		
		

		$(".remove_warning").bind(
				"click",
				function() {
					// 资源属性
					var warning_id = $(this).val();
					ajax.delete("/warninginfo/delete?warninginfoId="+ warning_id, "删除告警信息成功", "删除告警信息失败");
				});

		$(".edit_warning").bind(
				"click",
				function() {
					var warning_concepts = ajax.load(
							"/conceptcode?classification=2", "查询标签失败");
					App.seSelectConcept("warning_classification",
							warning_concepts);

					var level_concepts = ajax.load(
							"/conceptcode?classification=3", "查询标签失败");
					App.seSelectConcept("level_classification",
							level_concepts);
					
					loadArea();
					// 资源属性
					var warning_id = $(this).val();
					$("#editWarningInfoBtn").removeClass("hide");
					$("#addWarningInfoBtn").addClass("hide");
					var warninginfo = ajax.load("/warninginfo?warningInfoId=" + warning_id);
					$("#warning_id").val(warninginfo.warning_id);
					$("#warning_classification").val(warninginfo.warning_concept);
					$("#level_classification").val(warninginfo.level_concept);
					$("#warning_content").val(warninginfo.warning_content);
					$("#area_id").val(warninginfo.area_id);
					//
					var duration_time = getFormatDateByLong(warninginfo.start_time, "yyyy-MM-dd hh:mm") 
						+ "~" + getFormatDateByLong(warninginfo.end_time, "yyyy-MM-dd hh:mm");
					$("#duration_time").val(duration_time);
					// ###################
					$("#addDialog").modal('show');
				});

		$("#editWarningInfoBtn").bind(
				"click",
				function() {
					$("#addDialog").modal('hide');
					var warning_id = $("#warning_id").val();
					var warning_classification = $(
					'#warning_classification option:selected').val();
				if (warning_classification == "unselect") {
					App.myDialog("请选择预警类型", "提示");
					return;
				}
				var level_classification = $(
						'#level_classification option:selected').val();
				if (level_classification == "unselect") {
					App.myDialog("请选择预警级别", "提示");
					return; 
				}
				var warning_content = $("#warning_content").val();
				var area_id = $('#area_id option:selected').val();
				if (area_id == "unselect") {
					App.myDialog("请选择区域信息", "提示");
					return; 
				}
				var duration_time = $("#duration_time").val();
				var start_time = duration_time.split("~")[0] + ":00";
				var end_time = duration_time.split("~")[1] + ":00";

					var warninginfo = {
						warning_id: warning_id,
						warning_classification : "2",
						level_classification : "3",
						warning_concept : warning_classification,
						level_concept : level_classification,
						warning_content : warning_content,
						area_id : area_id,
						start_time : start_time,
						end_time : end_time
					}
					var warninginfoString = JSON.stringify(warninginfo);
					ajax.addOrUpdate("/warninginfo/edit", warninginfoString, "修改预警信息成功！", "修改预警信息失败：");
				});
	}
};
$(function() {
	controller.init();
});